package com.example.demo.controller;

import com.example.demo.exception.MyUncheckedException;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.SubjectFileDTO;
import com.example.demo.pojo.entity.SubjectFile;
import com.example.demo.pojo.query.BankQuery;
import com.example.demo.pojo.query.FileQuery;
import com.example.demo.pojo.vo.QuestionVO;
import com.example.demo.service.imply.ISubjectFileServiceImply;
import com.example.demo.service.imply.ISubjectServiceImply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fjh
 */
@RestController
@RequestMapping("/subjectFile")
@Slf4j
@Api(tags = "学科文件相关接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class SubjectFileController {
    private final ISubjectFileServiceImply subjectFileService;
    @ApiOperation("新键文件夹")
    @PostMapping("/add")
    public Result filter(SubjectFileDTO subjectFileDTO){
        int result=subjectFileService.addFile(subjectFileDTO);
        if(result==1){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
    @ApiOperation("初始化文件列表")
    @PostMapping("/init")
    public Result initFile(FileQuery fileQuery){
        return Result.success(subjectFileService.initFile(fileQuery));
    }
    @ApiOperation("根据Id进行查找文件")
    @PostMapping("/searchById")
    public Result searchById(int fileId){
        SubjectFile subjectFile=subjectFileService.searchById(fileId);
        return Result.success(subjectFile);
    }
    @ApiOperation("复制文件")
    @PostMapping("/copy")
    public Result copyFile(int subjectFileId,int goalSubjectFileId){
        if(subjectFileId==goalSubjectFileId){
            return Result.error("你选择了2个相同的目录");
        }
        SubjectFile subjectFile=subjectFileService.copySubjectFile(subjectFileId,goalSubjectFileId);
        return Result.success(subjectFile);
    }
    @ApiOperation("移动文件")
    @PostMapping("/shift")
    public Result shiftFile(int subjectFileId,int goalSubjectFileId){
        if(subjectFileId==goalSubjectFileId){
            return Result.error("你选择了2个相同的目录");
        }
        SubjectFile subjectFile=subjectFileService.shiftSubjectFile(subjectFileId,goalSubjectFileId);
        return Result.success(subjectFile);
    }
    @ApiOperation("更新文件")
    @PostMapping("/update")
    public Result updateFile(SubjectFileDTO subjectFileDTO){
        System.out.println("接收到的参数");
        System.out.println(subjectFileDTO);
        SubjectFile subjectFile=subjectFileService.updateFile(subjectFileDTO);
        if(subjectFile==null){
            return Result.error("更新失败");
        }
        return Result.success(subjectFile);
    }
    @ApiOperation("删除文件")
    @PostMapping("/delete")
    public Result deleteFile(int fileId) throws MyUncheckedException {
        System.out.println("接收到的参数");
        System.out.println(fileId);
        boolean flag=subjectFileService.deleteFile(fileId);
        if(!flag){
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
